import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  introduction: '',
  roles: [],
  dbname: '',
  id: '',
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_DBNAME: (state, dbname) => {
    state.dbname = dbname
  },
}

const actions = {
  async login({ commit }, userInfo) {
    console.log('store>user>login:', userInfo)
    try {
      let res = await login(userInfo)
      // console.log('store>user>login?res.data:', res.data)
      commit('SET_TOKEN', res.data)
      setToken(res.data)
      return '验证成功！'
    } catch (e) {
      return e
    }
  },

  // get user info
  async getInfo({ commit, state }) {
    try {
      // let res = await getInfo()
      // const { name, id, roles, funclist, dbname } = res.data

      const { name, id, roles, funclist, dbname } = {
        name: '管理12',
        id: '1001',
        roles: ['管理员', '文员'],
        funclist: [
          {
            gid: 'D61F9-B2947-B814D-F88E9-BBBB',
            parentid: '00000-00000-00000-00000-00000',
            path: '/machine-management',
            component: 'Layout',
            redirect: 'noRedirect',
            name: 'machine-management',
            meta: {
              title: '实时监控',
              icon: 'su_machine',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-JJJJ',
                parentid: 'D61F9-B2947-B814D-F88E9-BBBB',
                path: 'allFactory',
                component: 'machine-management/allFactory/index',
                name: 'allFactory',
                meta: { title: '整司概况', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-JJJJ',
                parentid: 'D61F9-B2947-B814D-F88E9-BBBB',
                path: 'workShop',
                component: 'machine-management/workShop/index',
                name: 'workShop',
                meta: { title: '车间概况', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-JJJJJ',
                parentid: 'D61F9-B2947-B814D-F88E9-BBBB',
                path: 'machineInfoRun',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal3',
                meta: { title: '运行情况', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-BBBB',
                parentid: 'D61F9-B2947-B814D-F88E9-BBBB',
                path: 'machineIntro',
                component: 'machine-management/machineIntro/index',
                name: 'machineIntro',
                meta: { title: '机台详情', icon: 'zip', roles: ['editor'] },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-CCCC',
                parentid: 'D61F9-B2947-B814D-F88E9-BBBB',
                path: 'spindle',
                component: 'machine-management/spindle/index',
                name: 'Spindle',
                meta: { title: '锭子详情', icon: 'zip' },
              },
            ],
          },

          {
            gid: 'D61F9-B2947-B814D-F88E9-DDDD',
            parentid: '00000-00000-00000-00000-00000',
            component: 'Layout',
            redirect: 'noRedirect',
            path: '/reports',
            name: 'machine-except',
            meta: {
              title: '数据统计',
              icon: 'su_except',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-IIII',
                parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
                path: 'productTotal',
                component: 'machine-management/productTotal/index',
                name: 'ProductTotal',
                meta: { title: '产量统计', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-IIIII',
                parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
                path: 'charts',
                component: 'machine-management/charts/index',
                name: 'Charts',
                meta: { title: '图表报表', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-IIIIII',
                parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
                path: 'machineInfo',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal4',
                meta: { title: '数据报表', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-IIIIIII',
                parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
                path: 'allBroken',
                component: 'machine-management/allBroken/index',
                name: 'ProductTotal9',
                meta: { title: '断头报表', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-IIIII1',
                parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
                path: 'exceptional',
                component: 'machine-management/exceptional/index',
                name: 'Exceptional',
                meta: { title: '异常报表', icon: 'zip' },
              },
            ],
          },
          // {
          //   gid: 'D61F9-B2947-B814D-F88E9-AAAA',
          //   parentid: '00000-00000-00000-00000-00000',
          //   path: '/machine-setting',
          //   component: 'Layout',
          //   redirect: 'noRedirect',
          //   name: 'machine-setting',
          //   meta: {
          //     title: '参数配置',
          //     icon: 'su_upload',
          //   },
          //   children: [
          //     {
          //       gid: 'C62E3-E76C0-48620-E8346-DDDD',
          //       parentid: 'D61F9-B2947-B814D-F88E9-AAAA',
          //       path: 'bdConfig',
          //       component: 'machine-management/bdConfig/index',
          //       name: 'BdConfig',
          //       meta: { title: '配置下发', icon: 'zip' },
          //     },
          //   ],
          // },
          {
            gid: 'D61F9-B2947-B814D-F88E9-BC80CP',
            parentid: '00000-00000-00000-00000-00000',
            path: '/system2',
            component: 'Layout',
            redirect: 'noRedirect',
            meta: {
              title: '机台设置',
              icon: 'rich-text',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C67',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CP',
                path: 'machineArgs',
                component: 'machine-management/machineArgs/index',
                name: 'Profile1',
                meta: { title: '机台参数', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C666',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CP',
                path: 'machineInfo2',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal5',
                meta: { title: '机台纺纱', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C666',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CP',
                path: 'machineInfo3',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal6',
                meta: { title: '触发设置', icon: 'zip' },
              },
            ],
          },
          {
            gid: 'D61F9-B2947-B814D-F88E9-BC80CP',
            parentid: '00000-00000-00000-00000-00000',
            path: '/system3',
            component: 'Layout',
            redirect: 'noRedirect',
            meta: {
              title: '纺纱产品',
              icon: 'rich-text',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C67',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CP',
                path: 'machineInfo4',
                component: 'machine-management/machineInfo/index',
                name: 'Profile2',
                meta: { title: '粗纱信息', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C666',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CP',
                path: 'machineInfo5',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal7',
                meta: { title: '细纱信息', icon: 'zip' },
              },
            ],
          },
          {
            gid: 'D61F9-B2947-B814D-F88E9-BC80D',
            parentid: '00000-00000-00000-00000-00000',
            path: '/business-baseinfo',
            component: 'Layout',
            redirect: 'noRedirect',
            name: 'businessBaseinfo',
            meta: {
              title: '人员班组',
              icon: 'peoples',
            },
            children: [
              {
                gid: 'ADE92-662A9-ECA2A-A366A-9D7C6',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
                path: 'userinfo6',
                component: 'business-baseinfo/userinfo/index',
                redirect: null,
                name: 'UserInfo7',
                meta: {
                  title: '员工信息',
                  icon: 'user',
                },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C66',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
                path: 'group',
                component: 'business-baseinfo/group/index',
                name: 'Group',
                meta: { title: '班组信息', icon: 'tree' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C67',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
                path: 'bcinfo',
                component: 'business-baseinfo/bcinfo/index',
                name: 'Bcinfo',
                meta: { title: '班次信息', icon: 'tree' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C68',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
                path: 'pbinfo',
                component: 'business-baseinfo/pbinfo/index',
                name: 'Pbinfo',
                meta: { title: '排班信息', icon: 'tree' },
              },

              // {
              //   gid: 'C62E3-E76C0-48620-E8346-9D7C661',
              //   parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
              //   path: 'machineInfo',
              //   component: 'machine-management/machineInfo/index',
              //   name: 'ProductTotal2',
              //   meta: { title: '班次信息', icon: 'zip' },
              // },

              // {
              //   gid: 'C62E3-E76C0-48620-E8346-9D7C662',
              //   parentid: 'D61F9-B2947-B814D-F88E9-BC80D',
              //   path: 'machineInfo',
              //   component: 'machine-management/machineInfo/index',
              //   name: 'ProductTotal2',
              //   meta: { title: '排班信息', icon: 'zip' },
              // },
            ],
          },
          {
            gid: 'D61F9-B2947-B814D-F88E9-CCCC',
            parentid: '00000-00000-00000-00000-00000',
            path: '/machine-debug',
            component: 'Layout',
            redirect: 'noRedirect',
            name: 'machine-debug',
            meta: {
              title: '运维调试',
              icon: 'su_debug',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-EEEE',
                parentid: 'D61F9-B2947-B814D-F88E9-CCCC',
                path: 'rawData',
                component: 'machine-management/rawData/index',
                name: 'RawData',
                meta: { title: '原始数据', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-DDDD',
                parentid: 'D61F9-B2947-B814D-F88E9-CCCC',
                path: 'bdConfig',
                component: 'machine-management/bdConfig/index',
                name: 'BdConfig',
                meta: { title: '配置下发', icon: 'zip' },
              },
              // {
              //   gid: 'C62E3-E76C0-48620-E8346-GGGG',
              //   parentid: 'D61F9-B2947-B814D-F88E9-CCCC',
              //   path: 'machineDetail',
              //   component: 'machine-management/machineDetail/index',
              //   name: 'MachineDetail',
              //   meta: { title: '机台详情', keepAlive: true, icon: 'zip' },
              // },
              // {
              //   gid: 'C62E3-E76C0-48620-E8346-HHHH',
              //   parentid: 'D61F9-B2947-B814D-F88E9-CCCC',
              //   path: 'allBroken',
              //   component: 'machine-management/allBroken/index',
              //   name: 'AllBroken',
              //   meta: { title: '全部断头', keepAlive: true, icon: 'zip' },
              // },
            ],
          },

          {
            gid: 'D61F9-B2947-B814D-F88E9-BC80CA',
            parentid: '00000-00000-00000-00000-00000',
            path: '/system1',
            component: 'Layout',
            redirect: 'noRedirect',
            meta: {
              title: '系统管理',
              icon: 'rich-text',
            },
            children: [
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C67',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CA',
                path: 'Profile3',
                component: 'profile/index',
                name: 'Profile3',
                meta: { title: '用户信息', icon: 'zip' },
              },
              {
                gid: 'C62E3-E76C0-48620-E8346-9D7C666',
                parentid: 'D61F9-B2947-B814D-F88E9-BC80CA',
                path: 'machineInfo',
                component: 'machine-management/machineInfo/index',
                name: 'ProductTotal8',
                meta: { title: '权限设置', icon: 'zip' },
              },
            ],
          },
          // {
          //   gid: 'D61F9-B2947-B814D-F88E9-DDDD',
          //   parentid: '00000-00000-00000-00000-00000',
          //   path: '/machineDetail',
          //   component: 'Layout',
          //   redirect: 'noRedirect',
          //   name: 'machineDetail',
          //   meta: {
          //     title: '新增页面',
          //     icon: 'su_debug',
          //   },
          //   children: [
          //     {
          //       gid: 'C62E3-E76C0-48620-E8346-GGGG',
          //       parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
          //       path: 'machineDetail',
          //       component: 'machine-management/machineDetail/index',
          //       name: 'MachineDetail',
          //       meta: { title: '机台详情', keepAlive: true, icon: 'zip' },
          //     },
          //     {
          //       gid: 'C62E3-E76C0-48620-E8346-HHHH',
          //       parentid: 'D61F9-B2947-B814D-F88E9-DDDD',
          //       path: 'allBroken',
          //       component: 'machine-management/allBroken/index',
          //       name: 'AllBroken',
          //       meta: { title: '全部断头', keepAlive: true, icon: 'zip' },
          //     },
          //     {
          //       gid: 'C62E3-E76C0-48620-E8346-JJJJ',
          //       parentid: 'D61F9-B2947-B814D-F88E9-GGGG',
          //       path: 'machineInfo',
          //       component: 'machine-management/machineInfo/index',
          //       name: 'machineInfo',
          //       meta: { title: '机台状态', keepAlive: true, icon: 'zip' },
          //     },
          //   ],
          // },
          // {
          //   gid: 'D61F9-B2947-B814D-F88E9-BC802',
          //   parentid: '00000-00000-00000-00000-00000',
          //   path: '/iplatform',
          //   component: 'Layout',
          //   redirect: 'noRedirect',
          //   name: 'iplatform',
          //   meta: {
          //     title: '实施平台',
          //     icon: 'el-icon-s-tools',
          //   },
          //   children: [
          //     {
          //       gid: 'ADE92-662A9-ECA2A-A366A-9D7C6',
          //       parentid: 'C62E3-E76C0-48620-E8346-A9A52',
          //       path: 'formdesign',
          //       component: '_xform/index',
          //       redirect: null,
          //       name: 'formdesign',
          //       meta: {
          //         title: '表单设计',
          //         icon: 'el-icon-s-operation',
          //       },
          //       children: null,
          //     },
          //     {
          //       gid: 'ADE92-662A9-ECA2A-A366A-9D7C7',
          //       parentid: 'C62E3-E76C0-48620-E8346-A9A52',
          //       path: 'FormManage',
          //       component: '_xform/FormManage',
          //       redirect: null,
          //       name: 'FormManage',
          //       meta: {
          //         title: '表单管理',
          //         icon: 'el-icon-s-operation',
          //       },
          //       children: null,
          //     },
          //     {
          //       gid: 'ADE92-662A9-ECA2A-A366A-9D7C8',
          //       parentid: 'C62E3-E76C0-48620-E8346-A9A52',
          //       path: 'mirco_vue/*',
          //       component: '_microapp/index',
          //       redirect: null,
          //       name: 'mirco_vue',
          //       meta: {
          //         title: '子应用测试',
          //         icon: 'el-icon-s-operation',
          //       },
          //       children: null,
          //     },
          //   ],
          // },
        ],
      }

      commit('SET_ROLES', roles)
      commit('SET_NAME', name)
      commit('SET_ID', id)
      commit('SET_DBNAME', dbname)
      commit('SET_AVATAR', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif')
      commit('SET_INTRODUCTION', '')
      return funclist
    } catch (e) {
      return e
    }
  },

  // user logout
  async logout({ commit, state, dispatch }) {
    try {
      // await logout()
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resetRouter()
      dispatch('tagsView/delAllViews', null, { root: true })
    } catch (e) {
      console.log('logout err:', e)
      return e
    }
  },

  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({ commit, dispatch }, role) {
    const token = role + '-token'
    commit('SET_TOKEN', token)
    setToken(token)

    const { roles } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, { root: true })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
